//
//  FourthViewController.m
//  A段
//
//  Created by 月月 on 15/4/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FourthViewController.h"
#import "UIScrollView+PullLoad.h"
#import "DisCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "MyConnection.h"
#import "FourthDetailCollectionViewCell.h"
#import "FourthDetailViewController.h"
#define F 0.95
#define BACKGROUNDCOLOR [UIColor colorWithRed:F green:F blue:F alpha:F]
#define HTTPLOG1 @"http://watch-cdn.idailywatch.com/api/list/video/zh-hans"

@interface FourthViewController () <PullDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *mycollectionView;
@property (nonatomic, retain) NSMutableArray *fourthArray;
@property (nonatomic, assign) NSInteger count;
@end

@implementation FourthViewController

- (void)dealloc
{
    [_mycollectionView release];
    [_fourthArray release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.fourthArray = [NSMutableArray array];
        self.count = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatMyFace];
    [self startConnect];
}

#pragma mark -
#pragma mark 搭建界面

- (void)creatMyFace
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(170, 160);
    [layout setSectionInset:UIEdgeInsetsMake(20, 10, 0, 0)];
    [layout setMinimumLineSpacing:30];
    self.mycollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    [_mycollectionView setDelegate:self];
    [_mycollectionView setDataSource:self];
    // 上拉,下拉
    [_mycollectionView setPullDelegate:self];
    _mycollectionView.canPullDown = YES;
    _mycollectionView.canPullUp = YES;
    
    [self.view addSubview:_mycollectionView];
    [_mycollectionView setBackgroundColor:BACKGROUNDCOLOR];
#warning 暂时先用DisCollectionCell
    [_mycollectionView registerClass:[DisCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_mycollectionView release];
    [layout release];
    
}

#pragma mark - 
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.fourthArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DisCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *dic = [_fourthArray objectAtIndex:indexPath.item];
    
    // 图片
    NSString *s = [dic objectForKey:@"cover_thumb"];
    NSURL *url = [NSURL URLWithString:s];
    
    [cell.imageView sd_setImageWithURL:url];
    
    // 日期
    cell.dataLabel.text = [dic objectForKey:@"pubdate"];
    // 标题
    cell.titleLabel.text = [dic objectForKey:@"title"];
    
    //设置cell的阴影
//    [cell.backgroundView.layer setShadowColor:[[UIColor blackColor] CGColor]];
//    [cell.backgroundView.layer setShadowOffset:CGSizeMake(20, 20)];
    return cell;
}

#pragma mark -
#pragma mark 网络请求数据
- (void)startConnect
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"page", @"iphone", @"ver", @"9", @"app_ver", nil];
    [MyConnection connectWithUrl:HTTPLOG1 parmaters:dic finishBlock:^(NSData *data) {
        self.fourthArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [_fourthArray removeLastObject];
        [_mycollectionView reloadData];
    }];
}

#pragma mark -
#pragma mark 上拉加载, 下拉刷新
- (void)scrollView:(UIScrollView *)scrollView loadWithState:(LoadState)state
{
    if (state == PullDownLoadState) {
        // 判断为下拉刷新, 2秒后执行downLoad方法
        [self getConnect];
    }else{
        // 判断为上拉加载, 2秒后执行方法
        [self loadMore];
    }
    
}

// 下拉刷新
- (void)getConnect
{
    self.count = 1;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"page", @"iphone", @"ver", @"9", @"app_ver", nil];
    
    [MyConnection connectWithUrl:HTTPLOG1 parmaters:dic finishBlock:^(NSData *data) {
        self.fourthArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [_fourthArray removeLastObject];
        [_mycollectionView reloadData];
        [_mycollectionView setFrame:CGRectMake(0, 54, self.view.frame.size.width, self.view.frame.size.height - 64)];
        [_mycollectionView stopLoadWithState:PullDownLoadState];
        
    }];
    
}

// 上来加载
- (void)loadMore
{
    self.count++;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld", self.count], @"page", @"iphone", @"ver", @"9", @"app_ver", nil];
    
    [MyConnection connectWithUrl:HTTPLOG1 parmaters:dic finishBlock:^(NSData *data) {
        NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [array removeLastObject];
        [self.fourthArray addObjectsFromArray:array];

        [_mycollectionView reloadData];
        [_mycollectionView stopLoadWithState:PullUpLoadState];
        
    }];
    
}


#pragma mark -
#pragma mark 点击跳转到详情界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    FourthDetailViewController *deta = [[FourthDetailViewController alloc] init];
    deta.detailArray = self.fourthArray;
    NSInteger click = indexPath.item;
    deta.click = click;
    [self.navigationController pushViewController:deta animated:YES];
    [deta release];
}



@end
